package com.example.wacoffee_user.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wacoffee_user.mapper.MeMapper;
import com.example.wacoffee_user.pojo.dto.rechargeDTO;
import com.example.wacoffee_user.pojo.entity.User;
import com.example.wacoffee_user.pojo.entity.coupon;
import com.example.wacoffee_user.service.IMeService;
import com.example.wacoffee_user.utils.CurrentHoler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MeServiceImpl extends ServiceImpl<MeMapper, User> implements IMeService {

    @Autowired
    private MeMapper meMapper;
    @Override
    public User login(User user) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",user.getPhone()).eq("password",user.getPassword());
        User one = getOne(queryWrapper);
        if(BeanUtil.isEmpty(one))throw new RuntimeException("手机号或密码错误");
        else return one ;
    }

    @Override
    public User getMe() {
        return meMapper.getMe(CurrentHoler.getCurrentId());
    }

    @Override
    public void addRechargeOrder(double amount) {
        meMapper.addRechargeOrder(amount,CurrentHoler.getCurrentId(), LocalDateTime.now());
    }

    @Override
    public List<rechargeDTO> getRechargeOrder() {
        return meMapper.getRechargeOrder(CurrentHoler.getCurrentId());
    }

    @Override
    public void updateInformationById(User user) {
        meMapper.updateInformationById(user);
    }

    @Override
    public Integer getCouponCount() {
        return  meMapper.getCouponCount(CurrentHoler.getCurrentId());
    }

    @Override
    public List<coupon> getMyCoupon() {
        return meMapper.getMyCoupon(CurrentHoler.getCurrentId());
    }

    @Override
    public void checkAndUpdate(Integer currentId) {
        meMapper.checkAndUpdateLevel(currentId);
        meMapper.addCoupon(currentId,53);
    }


}
